package com.fita.admin.web.controller.data;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.resolver.Preloading;
import com.demo2do.core.web.utils.WebUtils;
import com.fita.admin.entity.search.data.LocationSearch;
import com.fita.admin.service.DataService;
import com.fita.admin.service.data.CountryService;
import com.fita.entity.data.Country;
import com.fita.entity.data.Location;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 门店管理
 *
 * @author Ark
 *
 */
@Controller
@RequestMapping("/stores")
@MenuSetting("menu-data")
public class StoreController {
    
    private Logger logger = LoggerFactory.getLogger(StoreController.class);
    
    @Autowired
    @Qualifier("dataService")
    private DataService<Location> locationService;
    
    @Autowired
    private CountryService countryService;

    /**
     * search page
     * @return
     */
    @RequestMapping(path = "/search", method = RequestMethod.GET)
    @MenuSetting("submenu-store-management")
    public ModelAndView search() {
    	
    	ModelAndView modelAndView = new ModelAndView("data/store/store-search");
    	
    	List<Country> countries = countryService.loadAll(Country.class);
    	
    	modelAndView.addObject("countries", countries);
    	
    	return modelAndView;
    }
    
    @RequestMapping(path = "", method = RequestMethod.GET, params = "filter")
    @MenuSetting("submenu-store-management")
    public ModelAndView onSearch(HttpServletRequest request, LocationSearch search, Page page) {
    	
    	ModelAndView modelAndView = new ModelAndView("data/store/store-list");
    	
    	int totals = locationService.count(Location.class, search.getPreparedFilter(false));
    	
    	if (totals > 0) {
    		
			List<Location> locations = locationService.search(Location.class, search, page.with(totals));
			
			modelAndView.addObject("locations", locations);
            modelAndView.addObject("page", page);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page", "filter"));
		}
    	return modelAndView;
    }
    
    
    /**
     * list
     *
     * @return
     */
    @RequestMapping(value = "" , method = RequestMethod.GET)
    @MenuSetting("submenu-store-management")
    public ModelAndView list(Page page) {
                      
        ModelAndView modelAndView = new ModelAndView("data/store/store-list");
        
        int totals = locationService.count(Location.class);
        if (totals > 0) {

            List<Location> locations = locationService.listByPage(Location.class, page.with(totals));
            
            modelAndView.addObject("locations", locations);
            modelAndView.addObject("page", page);
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @MenuSetting("submenu-store-management")
    public ModelAndView view(@PathVariable("id") Long id) {
        
        ModelAndView modelAndView = new ModelAndView("data/store/store-view");
        
        Location location = locationService.load(Location.class, id);
        
        modelAndView.addObject("location", location);
        
        return modelAndView;
        
    }
    
    /**
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
    public ModelAndView edit(@PathVariable("id") Long id) {
        
        ModelAndView modelAndView = new ModelAndView("data/store/store-edit");
        
        Location location = locationService.load(Location.class, id);
        
        modelAndView.addObject("location", location);
        
        return modelAndView;
        
    }
    
    /**
     * 
     * @param id
     * @param location
     * @return
     */
    @RequestMapping(value = "/{id}" , method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onEdit(@PathVariable(name="id") Long id, @Preloading("locationId") Location location) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            locationService.update(location);
            
            httpResult.success().message("编辑门店成功");
            
        } catch (Exception e) {
            httpResult.fail("500").message("编辑门店失败");
            logger.error("#onEdit() - edit fail, exception is [{}]",e);
        }
        
        return httpResult;
        
    }
}
